﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

namespace FrameWork
{

    public class TouchHandle
    {
        private event OnTouchEventHandle eventHandle = null;
        private object[] handleParams;

        public void DestoryHandle()
        {
            if (null != eventHandle)
            {
                eventHandle -= eventHandle;
                eventHandle = null;
            }
        }

        public void CallEventHandle(EventTriggerListener _listener,object _args)
        {
            if (null != eventHandle)
            {
                eventHandle(_listener,_args,handleParams);
            }
        }

        public void SetEventHandle(OnTouchEventHandle _handle,params object[] _params)
        {
            DestoryHandle();
            eventHandle += _handle;
            handleParams = _params;
        }
        
        public TouchHandle(OnTouchEventHandle _handle,params object[] _params)
        {
            SetEventHandle(_handle, _params);
        }
        public TouchHandle()
        {

        }
    }

    public class EventTriggerListener : MonoBehaviour,
        IPointerClickHandler,
        IPointerDownHandler,
        IPointerUpHandler,
        IPointerEnterHandler,
        IPointerExitHandler,
        ISelectHandler,
        IUpdateSelectedHandler,
        IDeselectHandler,
        IDragHandler,
        IEndDragHandler,
        IDropHandler,
        IScrollHandler,
        IMoveHandler
    {
        public TouchHandle onClick;
        public TouchHandle onDoubleClick;
        public TouchHandle onDown;
        public TouchHandle onEnter;
        public TouchHandle onExit;
        public TouchHandle onUp;
        public TouchHandle onSelect;
        public TouchHandle onUpdateSelect;
        public TouchHandle onDeselect;
        public TouchHandle OnDrag;
        public TouchHandle onDragEnd;
        public TouchHandle onDrop;
        public TouchHandle onScroll;
        public TouchHandle onMove;

        // Use this for initialization
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }

        //获取EventTriggerListener组件
        static public EventTriggerListener Get(GameObject go)
        {
//             EventTriggerListener listener = go.GetOrAddComponent<EventTriggerListener>();
//             return listener;
            return go.GetOrAddComponent<EventTriggerListener>();
        }

        void OnDestory()
        {
            ReMoveAllHandle();
        }

        private void ReMoveAllHandle()
        {
            this.ReMoveHandle(onClick);
            this.ReMoveHandle(onDoubleClick);
            this.ReMoveHandle(onDown);
            this.ReMoveHandle(onEnter);
            this.ReMoveHandle(onExit);
            this.ReMoveHandle(onUp);
            this.ReMoveHandle(onDrop);
            this.ReMoveHandle(OnDrag);
            this.ReMoveHandle(onDragEnd);
            this.ReMoveHandle(onScroll);
            this.ReMoveHandle(onMove);
            this.ReMoveHandle(onUpdateSelect);
            this.ReMoveHandle(onSelect);
            this.ReMoveHandle(onDeselect);
        }

        private void ReMoveHandle(TouchHandle _handle)
        {
            if (null != _handle)
            {
                _handle.DestoryHandle();
                _handle = null;
            }             
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (null != onDoubleClick)
                onClick.CallEventHandle(this, eventData);
        }

//         public void OnPointerClick(PointerEventData eventData)
//         {
//             if (null != onClick)
//                 onClick.CallEventHandle(this, eventData);
//         }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (null != onDown)
                onClick.CallEventHandle(this, eventData);
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (null != onUp)
                onClick.CallEventHandle(this, eventData);
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            if (null != onEnter)
                onClick.CallEventHandle(this, eventData);
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            if (null != onExit)
                onClick.CallEventHandle(this, eventData);
        }

        public void OnSelect(BaseEventData eventData)
        {
            if (null != onSelect)
                onClick.CallEventHandle(this, eventData);
        }

        public void OnUpdateSelected(BaseEventData eventData)
        {
            if (null != onUpdateSelect)
                onClick.CallEventHandle(this, eventData);
        }

        void IDeselectHandler.OnDeselect(BaseEventData eventData)
        {
            if (null != onDeselect)
                onClick.CallEventHandle(this, eventData);
        }

        void IDragHandler.OnDrag(PointerEventData eventData)
        {
            if (null != OnDrag)
                onClick.CallEventHandle(this, eventData);
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (null != onDragEnd)
                onClick.CallEventHandle(this, eventData);
        }

        public void OnDrop(PointerEventData eventData)
        {
            if (null != onDrop)
                onClick.CallEventHandle(this, eventData);
        }

        void IScrollHandler.OnScroll(PointerEventData eventData)
        {
            if (null != onScroll)
                onClick.CallEventHandle(this, eventData);
        }

        public void OnMove(AxisEventData eventData)
        {
            if (null != onMove)
                onClick.CallEventHandle(this, eventData);
        }

        public void SetEventHandle(EnumTouchEventType _type,OnTouchEventHandle _handle,params object[] _params)
        {
            switch(_type)
            {
                case EnumTouchEventType.OnClick:
                    {
                        if (null == onClick)
                        {
                            onClick = new TouchHandle();
                        }
                        onClick.SetEventHandle(_handle, _params);
                    }
                    break;
                case EnumTouchEventType.OnDoubleClick:
                    {
                        if (null == onDoubleClick)
                        {
                            onDoubleClick = new TouchHandle();
                        }
                        onDoubleClick.SetEventHandle(_handle, _params);
                    }
                    break;
                case EnumTouchEventType.OnDrag:
                    break;
                default:
                    break;
            }
        }

    }

}

